/* $Id: ServiceReqDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.ConstantsCommon.RequestType;
import ae.pcfc.etks.online.common.dao.ServiceReqDAO;
import ae.pcfc.etks.online.common.domain.AppMetaData;
import ae.pcfc.etks.online.common.domain.ServiceReq;

@Repository
public class ServiceReqDAOImpl extends BaseDAO implements ServiceReqDAO {

	/**
	 * addServiceReq
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ServiceReqDAO#addServiceReq(ae.pcfc.etks.online.common.domain.ServiceReq)
	 * 
	 */
	public void addServiceReq(ServiceReq serviceReq) {

		getHibernateTemplate().getSessionFactory().getCurrentSession().save(serviceReq);
	}

	/**
	 * findServiceReqByApplicationId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ServiceReqDAO#findServiceReqByApplicationId(java.lang.String)
	 * 
	 */
	public List<ServiceReq> findServiceReqByApplicationId(String applicationId) {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createCriteria(ServiceReq.class, "serviceReq");

		criteria.add(Restrictions.eq("serviceReq.applicationId", applicationId));

		@SuppressWarnings("unchecked")
		List<ServiceReq> list = (List<ServiceReq>) criteria.list();

		return list == null ? null : list;
	}

	/**
	 * findServiceReqByUserAndServiceId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ServiceReqDAO#findServiceReqByUserAndServiceId(java.lang.Long, java.lang.Long)
	 * 
	 */
	public ServiceReq findServiceReqByUserAndServiceId(Long userId, Long serviceId) {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createCriteria(ServiceReq.class, "serviceReq");
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		criteria.createAlias("serviceReq.user", "user");
		criteria.add(Restrictions.eq("user.UId", userId));
		criteria.createAlias("serviceReq.service", "service");
		criteria.add(Restrictions.eq("service.serviceId", serviceId));
		criteria.createAlias("service.serviceAttrs", "serviceAttrs");
		criteria.add(Restrictions.eq("serviceAttrs.serviceAttrStatus", ConstantsCommon.ACTIVE).ignoreCase());
		//criteria.add(Restrictions.eq("serviceReq.applicationId", new BigDecimal(applicationId)));
		/************** Sub Criteria for the Service appln which are already requested and rejected *****************/
		DetachedCriteria appMetaDataRequsted = DetachedCriteria.forClass(AppMetaData.class, "appMetaData");
		appMetaDataRequsted.setProjection(Projections.property("appMetaData.applicationId"));
		appMetaDataRequsted.add(Restrictions.like("appMetaData.clientRefNo", RequestType.SERVICE.getCode()+"%"));
		appMetaDataRequsted.add(Restrictions.ne("appMetaData.wfStatus", ConstantsCommon.REJECT));
		
		
		// Setting for the  Service appln  which are already registered and rejected exclude them
		criteria.add(Subqueries.propertyIn("serviceReq.applicationId", appMetaDataRequsted));	
		@SuppressWarnings("unchecked")
		List<ServiceReq> list = (List<ServiceReq>) criteria.list();

		return (list != null && list.size() > 0) ? list.get(0) : null;
	}

}
